<?php
/**
 * 后台首页需要用到的一些数据
 * User: crstu
 * Date: 2016/6/20
 * Time: 10:50
 */

namespace backend\models\Service;


use common\models\Cache;
use common\models\DAO\Collection;
use common\models\DAO\UserTradeList;

class IndexService
{

    //缓存过期时间
    const CACHE_EXPIRE_TIME = 300;

    /**
     * 用户总数
     */
    public static function totalUserNumber(){
        $total = Cache::get('TOTAL_USER_NUMBER');
        if(empty($total)){
            $total = \common\models\DAO\User::find()->count();
            Cache::set('TOTAL_USER_NUMBER', $total, self::CACHE_EXPIRE_TIME);
        }
        return $total;
    }


    /**
     * 成交额
     */
    public static function totalTradeMoney(){
        $total = Cache::get('TOTAL_TRADE_MONEY');
        if(empty($total)){
            $total = UserTradeList::find()->sum('price') + UserTradeList::find()->sum('sellPrice');
            Cache::set('TOTAL_TRADE_MONEY', $total, 300);
        }
        return $total;
    }


    /*
     * 成交笔数
     */
    public static function totalTradeCount(){
        $total = Cache::get('TOTAL_TRADE_COUNT');
        if(empty($total)){
            $total = UserTradeList::find()->count();
            Cache::set('TOTAL_TRADE_COUNT', $total, 300);
        }
        return $total;
    }

    /*
     * 总票数
     */
    public static function totalCollection(){
        $total = Cache::get('TOTAL_COLLECTION');
        if(empty($total)){
            $total = \common\models\DAO\UserTradeList::find()->count();
            Cache::set('TOTAL_COLLECTION', $total, 300);
        }
        return $total;
    }


    public static function totalChargeMoney(){
        $total = Cache::get('TOTAL_CHARGE_MONEY');
        if(empty($total)){
            $total = \common\models\DAO\UserCharge::find()->where(['typeID' => 1])->sum('money');
            Cache::set('TOTAL_CHARGE_MONEY', $total, 300);
        }
        return $total;
    }

}